<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryObj.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="hChange" />
        <!-- 树形组件 -->
        <!-- show-checkbox -->
        <!-- :expand-on-click-node="false" -->
        <el-tree
          ref="tree"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          highlight-current
          node-key="id"
          check-strictly
          @current-change="hCurrentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>
          <el-button v-allow="'excel_export'" size="mini" @click="hExport">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{row}">
              <img v-if="row.staffPhoto" style="width:30px" :src="row.staffPhoto">
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-if="row.formOfEmployment === 2">非正式</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/'+row.id)">查看</el-button>
              <el-button size="mini" type="text" @click="hAssignRose(row.id)">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" style="height:60px" align="middle" justify="end">
          <!-- 放置分页组件
        total: 总数据条数
        page-size: 每页几条数据
        @current-change： 页码变化（用户点击）执行，自动传入当前的页码
        -->
          <el-pagination
            :current-page="queryObj.page"
            :background="true"
            layout="total,prev, pager, next"
            :total="total"
            :page-size="queryObj.pagesize"
            @current-change="changePage"
          />
        </el-row>
      </div>
    </div>
    <ImportExcel
      :show-excel-dialog.sync="showExcelDialog"
      @updateEmployees="loadEmployment"
    />
    <assign-role ref="refRole" :show-role-dialog.sync="showRoleDialog" :cur-id="curId" />
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { arr2tree } from '@/utils'
import FileSaver from 'file-saver'
import { getEmployeeList, exportEmployee } from '@/api/employee'
import ImportExcel from './components/import-excel.vue'
import assignRole from '../employee/components/assignRole.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel, assignRole
  },
  data() {
    return {
      curId: -1, // 当前操作的员工的id值
      showRoleDialog: false,
      total: 0,
      showExcelDialog: false,
      // pagesize: 5,
      // page: 1,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      employList: []
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async hExport() {
      const res = await exportEmployee()
      console.log(res)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    changePage(newPage) {
      this.queryObj.page = newPage
      this.loadDepartment()
    },
    hChange() {
      clearTimeout(this.timeID)
      this.timeId = setTimeout(() => {
        this.queryObj.page = 1
        this.loadDepartment()
      }, 500)
    },
    async loadDepartment() {
      const res = await getDepartment()
      // console.log(res)
      // this.depts = res.data
      const rs = arr2tree(res.data, 0)
      this.depts = rs
      const id = rs[0].id
      // console.log(id)
      this.queryObj.departmentId = id
      this.$nextTick(() => {
        // this.$refs.tree.setCheckedKeys([id])
        this.$refs.tree.setCurrentKey(id)
      })
      this.loadEmployment()
    },

    async loadEmployment() {
      // console.log('查询条件', this.queryObj)
      // axios 在发请求时会自动丢弃值为 null  的属性
      const res = await getEmployeeList(this.queryObj)
      // console.log(res)
      this.total = res.data.total
      this.employList = res.data.rows
    },

    hCurrentChange(data, node) {
      console.log(data, node)
      this.queryObj.departmentId = data.id
      this.loadEmployment()
    },
    hAssignRose(id) {
      // 保存id值
      this.curId = id
      this.showRoleDialog = true
      // 调用子组件方法,获取当前员工详情
      this.$nextTick(() => {
        this.$refs.refRole.getRoseById()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>

<style>

/* .el-tree--highlight-current .el-tree-node.is-checked > .el-tree-node__content {
    color: #276FFB;
    background-color: #f0f4ff;
} */

</style>
